package org.jeecg.modules.demo.cashier;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.weixin.entity.SmtOnceCardInfo;
import org.jeecg.modules.demo.weixin.entity.SmtOnceCardUsers;
import org.jeecg.modules.demo.weixin.smtoncecardinfo.service.ISmtOnceCardInfoService;
import org.jeecg.modules.demo.weixin.smtoncecardusers.service.ISmtOnceCardUsersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
* @Description: 次卡表
* @Author: jeecg-boot
* @Date:   2023-10-28
* @Version: V1.0
*/
@Api(tags="次卡表")
@RestController
@RequestMapping("/weixin/cashierCardInfo/cashiercardinfo")
@Slf4j
public class CashierCardInfoController extends JeecgController<SmtOnceCardInfo, ISmtOnceCardInfoService> {
   @Autowired
   private ISmtOnceCardInfoService smtOnceCardInfoService;

    @Autowired
    private ISmtOnceCardUsersService smtOnceCardUsersService;
   /**
    * 分页列表查询
    *
    * @param smtOnceCardInfo
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   //@AutoLog(value = "smt_once_card_info-分页列表查询")
   @ApiOperation(value="smt_once_card_info-分页列表查询", notes="smt_once_card_info-分页列表查询")
   @GetMapping(value = "/list")
   public Result<IPage<SmtOnceCardInfo>> queryPageList(SmtOnceCardInfo smtOnceCardInfo,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<SmtOnceCardInfo> queryWrapper = QueryGenerator.initQueryWrapper(smtOnceCardInfo, req.getParameterMap());
       Page<SmtOnceCardInfo> page = new Page<SmtOnceCardInfo>(pageNo, pageSize);
       IPage<SmtOnceCardInfo> pageList = smtOnceCardInfoService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

    /**
     *   添用户次卡信息
     *
     * @param smtOnceCardUsers
     * @return
     */
    @AutoLog(value = "smt_once_card_users-添加")
    @ApiOperation(value="smt_once_card_users-添加", notes="smt_once_card_users-添加")
    @PostMapping(value = "/addUserCard")
    public Result<String> addUserCard(@RequestBody SmtOnceCardUsers smtOnceCardUsers) {
        if (!CollectionUtils.isEmpty(smtOnceCardUsers.getUserCardDtoList())){
            smtOnceCardUsers.getUserCardDtoList().forEach(userCardDto -> {
                SmtOnceCardUsers smtOnceCardUsers1 = new SmtOnceCardUsers();
                smtOnceCardUsers1.setMerId(smtOnceCardUsers.getMerId());
                smtOnceCardUsers1.setUserId(smtOnceCardUsers.getUserId());
                BeanUtils.copyProperties(userCardDto,smtOnceCardUsers1);
                smtOnceCardUsers1.setMerchantId(Integer.valueOf(smtOnceCardUsers.getMerId()));
                smtOnceCardUsersService.save(smtOnceCardUsers1);
            });
        }

        smtOnceCardUsersService.save(smtOnceCardUsers);
        return Result.OK("添加成功！");
    }

    /**
     * 分页列表查询
     *
     * @param smtOnceCardUsers
     * @param current
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "smt_once_card_users-分页列表查询")
    @ApiOperation(value="smt_once_card_users-分页列表查询", notes="smt_once_card_users-分页列表查询")
    @GetMapping(value = "/queryUserCard")
    public Result<IPage<SmtOnceCardUsers>> queryPageList(SmtOnceCardUsers smtOnceCardUsers,
                                                         @RequestParam(name="pageNo", defaultValue="1") Integer current,
                                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                         HttpServletRequest req) {
        QueryWrapper<SmtOnceCardUsers> queryWrapper = QueryGenerator.initQueryWrapper(smtOnceCardUsers, req.getParameterMap());
        queryWrapper.eq("merchant_id", Integer.valueOf(smtOnceCardUsers.getMerId()));
        Page<SmtOnceCardUsers> page = new Page<SmtOnceCardUsers>(current, pageSize);
        IPage<SmtOnceCardUsers> pageList = smtOnceCardUsersService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


}
